<?php
switch($do){
	case "add":
	case "edit":
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		$is_modify = isset($_REQUEST['is_modify']) ? $_REQUEST['is_modify']:'';
		if($is_submit){
			//DEBUG 接收用户信息并插入用户数据 start
            //DEBUG 机构部门 学校 班级 选择设置修改
            $d_string = isset($_REQUEST['d_string']) ? $_REQUEST['d_string']:'';
            //DEBUG 判断选择的是是否地区或学校班级
            $isarea = 1;//DEBUG 选择的是地区
            if(strpos($d_string,"_")){
                $isarea = 2;//选择的非地区
            }
            $d_string = trim($d_string,'_._');
            if(strpos($d_string,"_")){
                $d_array = explode('_',$d_string);
                $user_dept_id = $d_array[0];
                $class_id=$d_array[1];
            }else{
                $user_dept_id = $d_string;
                $class_id='';
            }
            //DEBUG 新增用户 判断机构不能为空
            if(empty($is_modify) && empty($user_dept_id)){
                $return_array = array("success"=>0,"message"=>"所属单位不能为空","data"=>array());
                echo json_encode($return_array);
                die;
            }
            //DEBUG 教师选择教授学科前端变更三级分类选择 开始
            $cate_ids = array();
            $cate_ids = isset($_REQUEST['cate_ids']) && !empty($_REQUEST['cate_ids']) ? implode(',',$_REQUEST['cate_ids']) : '';
            //DEBUG 教师选择教授学科前端变更三级分类选择 结束
			$data = array(
                'user_name'=> $_REQUEST['userInfo_user_name'],
				'user_realname'=> $_REQUEST['userInfo_user_realname'],
				'user_phone'=> $_REQUEST['userInfo_user_phone'],
				'user_email'=> $_REQUEST['userInfo_user_email'],
				'user_role_id'=> $_REQUEST['userInfo_user_role_id'],
				'parent_user_id'=> $_REQUEST['userInfo_parent_user_id'],
				'subjects'=> $cate_ids,
				'is_dept_master'=> $_REQUEST['userInfo_is_dept_master'],
			);
            //DEBUG 用户 判断账号不能为空
            if(empty($data['user_name'])){
                $return_array = array("success"=>0,"message"=>"账号不能为空","data"=>array());
                echo json_encode($return_array);
                die;
            }
            //DEBUG 用户 判断真实姓名不能为空
            if(empty($data['user_realname'])){
                $return_array = array("success"=>0,"message"=>"真实姓名不能为空","data"=>array());
                echo json_encode($return_array);
                die;
            }
            //DEBUG 新增 判断用户名不能重复
            if(empty($is_modify) && !empty($data['user_name'])){
                $hschool_user = load_class('hschool','user');
                $user_info = $hschool_user->get_info_by_user_name($data['user_name']);
                if($user_info['user_name']){
                    $return_array = array("success"=>0,"message"=>"账号已存在,请选择其他账号","data"=>array());
                    echo json_encode($return_array);
                    die;   
                }
            }
			if($class_id){
				$data['class_id']=$class_id;
			}
			if($user_dept_id){
				$data['dept_id']=$user_dept_id;
			}
			if(isset($_REQUEST['userInfo_user_password']) && !empty($_REQUEST['userInfo_user_password'])){
				$data['user_password'] = encode_password($_REQUEST['userInfo_user_password']);
			}
			if($is_modify){
                $data_update['user_name'] = $data['user_name'];
				$data_update['user_realname'] = $data['user_realname'];
				$data_update['user_phone'] = $data['user_phone'];
				$data_update['user_email'] = $data['user_email'];
				if($class_id){
					$data_update['class_id']=$class_id;
				}
				if($user_dept_id){
					$data_update['dept_id']=$user_dept_id;
				}
				if($data['user_password']){
					$data_update['user_password'] = $data['user_password'];
				}
				$user_subjects = $cate_ids;
				if($user_subjects){
					$data_update['subjects']=$user_subjects;
				}
				$data_update['user_modify_time'] = TIMESTAMP;
				$user_id = $_REQUEST['userInfo_user_id'];
                //DEBUG 编辑 判断用户名不能重复
                if(!empty($data_update['user_name'])){
                    $info_sql = "SELECT * FROM ".DB::table('users')." WHERE user_name='".$data_update['user_name']."' AND user_id <> '".$user_id."' LIMIT 1"; 
                    $info_array = DB::fetch_first($info_sql);
                    if($info_array['user_name']){
                        $return_array = array("success"=>0,"message"=>"账号已存在,请选择其他账号","data"=>array());
                        echo json_encode($return_array);
                        die;
                    }
                }
				$where = array(
					'user_id' => $user_id
				);
				//DEBUG 更新学生班级归属
				if($data_update['class_id']){
					@DB::update('class_member',array('class_id'=>$data_update['class_id']),$where);
                    //DEBUG 用户如果有班级更新当前班级教育经历则 更新教育经历表 开始
                    $school_info = array();
                    $hschool_school = load_class('hschool','school');
                    $school_info = $hschool_school->get_one($data_update['dept_id']);
                    $ue_data = array();
                    //$ue_data['ue_user_id']=$user_id;
                    $ue_data['ue_area_id1']=$school_info['area_id1'];
                    $ue_data['ue_area_id2']=$school_info['area_id2'];
                    $ue_data['ue_area_id3']=$school_info['area_id3'];
                    $ue_data['ue_school_type']=$school_info['school_type'];
                    $ue_data['ue_d_id']=$school_info['d_id'];
                    $ue_data['ue_school_name']=ext::d_name($school_info['d_id']);
                    $ue_data['ue_school_year']=date('Y');
                    $ue_data['ue_class_id']=$data_update['class_id'];
                    $ue_data['ue_class_name']=ext::class_name($data_update['class_id']);
                    //$ue_data['ue_in_school']=2;
                    $ue_data['ue_note']='';
                    $ue_data['create_time']=TIMESTAMP;
                    $ue_where = array('ue_user_id'=>$user_id,'ue_in_school'=>2);
                    $insert_users_edu_id = DB::update('users_edu',$ue_data,$ue_where);
                    //DEBUG 用户如果有班级更新当前班级教育经历则 更新教育经历表 结束
				}
                if($data_update['dept_id']){
                    $hschool_dept = load_class('hschool','dept');
                    $d_info = $hschool_dept->get_info($data['dept_id']);
                    if(1==$isarea){
                      $area = $hschool_dept->get_tree_path_id($data['dept_id']);  
                    }
                    if(2==$isarea){
                      $area = $hschool_dept->get_tree_path_id($d_info['d_parent_id']);  
                    }
                    $data_update['area_id1'] = isset($area[1]) ? $area[1]:0;
                    $data_update['area_id2'] = isset($area[2]) ? $area[2]:0;
                    $data_update['area_id3'] = isset($area[3]) ? $area[3]:0;
                }
				$effects = DB::update('users',$data_update,$where);
                //DEBUG 获取用户所属网校
                $user_role_id_sql = "SELECT user_role_id FROM ".DB::table('users')." WHERE user_id = '".$user_id."' LIMIT 1"; 
                $user_role_id_result = DB::result_first($user_role_id_sql);
                $d_string_org = isset($_REQUEST['d_string_org']) && !empty($_REQUEST['d_string_org']) ? trim($_REQUEST['d_string_org'],','):'';
                if($d_string_org){
                    @DB::delete('org_user',array('ou_user_id'=>$user_id));
                    $d_array_org = explode(',',$d_string_org);
                    //DEBUG 删除教师非勾选的签约记录 开始
                    if(empty($d_array_org)){
                        @DB::delete('apply_org',array('user_id'=>$user_id));
                    }else{
                        $sql = "DELETE FROM ".DB::table("apply_org")." WHERE user_id='".$user_id."' AND d_id NOT IN (".dimplode($d_array_org).")";
                        @DB::query($sql);
                    }
                    //DEBUG 删除教师非勾选的签约记录 结束
                    foreach($d_array_org AS $key => $value){
                        @DB::insert('org_user',array('ou_d_id'=>$value,'ou_user_id'=>$user_id,'user_role_id'=>$user_role_id_result));
                        //DEBUG 判断是否已经存在签约如果存在签约 把签约设置为审核通过 不存在签约则增加网校签约表(apply_org)信息
                        $check_apply_org = DB::result_first("SELECT id FROM ".DB::table("apply_org")." WHERE user_id='".$user_id."' AND d_id='".$value."' LIMIT 1");
                        if($check_apply_org){
                            @DB::update("apply_org",array('flag'=>1),array('user_id'=>$user_id,'d_id'=>$value));
                        }else{
                            @DB::insert("apply_org",array('user_id'=>$user_id,'d_id'=>$value,'dateline'=>TIMESTAMP,'flag'=>1));
                        }
                    }
                }else{
                    //DEBUG 取消网校的话 删除用户网校关系与签约
                    @DB::delete('org_user',array('ou_user_id'=>$user_id));
                    @DB::delete('apply_org',array('user_id'=>$user_id));
                }
			}else{
				$data['user_create_time'] = TIMESTAMP;
                //DEBUG 判断机构是否为空 如果为空默认继承 添加人的机构归属
                if(empty($data['dept_id'])){
                    $data['dept_id']=$_G['member']['dept_id'];
                }
                //DEBUG 增加获取用户地区属性
                $hschool_dept = load_class('hschool','dept');
                $d_info = $hschool_dept->get_info($data['dept_id']);
                if(1==$isarea){
                  $area = $hschool_dept->get_tree_path_id($data['dept_id']);  
                }
                if(2==$isarea){
                  $area = $hschool_dept->get_tree_path_id($d_info['d_parent_id']);  
                }
                $data['area_id1'] = isset($area[1]) ? $area[1]:0;
                $data['area_id2'] = isset($area[2]) ? $area[2]:0;
                $data['area_id3'] = isset($area[3]) ? $area[3]:0;
				$insert_user_id = DB::insert('users',$data,1);
                /*
                    $update = array(
                        'user_name'=> $insert_user_id
                    );
                    $where = array(
                        'user_id' => $insert_user_id
                    );
                    $effects = DB::update('users',$update,$where);
                 */
				//DEBUG 插入班级成员
				//$class_id = isset($_REQUEST['class_id']) && !empty($_REQUEST['class_id']) ? $_REQUEST['class_id']:'';
				if($class_id){
                    $user_schoolid=$data['dept_id'];
					$data = array(
						'class_id' => $class_id,
						'user_id' => $insert_user_id,
						'cm_type' => $_REQUEST['userInfo_user_role_id'],
						'subject_d_ids' => $data['subjects'],
						'create_time' => TIMESTAMP
					);
					$insert_id = DB::insert('class_member',$data,1);
                    //DEBUG 创建用户如果有班级插入教育经历表 开始
                    $school_info = array();
                    $hschool_school = load_class('hschool','school');
                    $school_info = $hschool_school->get_one($user_schoolid);
                    $ue_data = array();
                    $ue_data['ue_user_id']=$insert_user_id;
                    $ue_data['ue_area_id1']=$school_info['area_id1'];
                    $ue_data['ue_area_id2']=$school_info['area_id2'];
                    $ue_data['ue_area_id3']=$school_info['area_id3'];
                    $ue_data['ue_school_type']=$school_info['school_type'];
                    $ue_data['ue_d_id']=$school_info['d_id'];
                    $ue_data['ue_school_name']=ext::d_name($school_info['d_id']);
                    $ue_data['ue_school_year']=date('Y');
                    $ue_data['ue_class_id']=$class_id;
                    $ue_data['ue_class_name']=ext::class_name($class_id);
                    $ue_data['ue_in_school']=2;
                    $ue_data['ue_note']='';
                    $ue_data['create_time']=TIMESTAMP;
                    $insert_users_edu_id = DB::insert('users_edu',$ue_data,1);
                    //DEBUG 创建用户如果有班级插入教育经历表 结束
				}
				//DEBUG 添加家长 绑定孩子
				if($_REQUEST['userInfo_child_user_id']){
					//DEBUG 更新孩子的家长
					$post_child_user_id = isset($_REQUEST['userInfo_child_user_id']) && !empty($_REQUEST['userInfo_child_user_id']) ? trim($_REQUEST['userInfo_child_user_id']):'';
					@DB::update('users',array('parent_user_id'=>$insert_user_id)," user_id='".$post_child_user_id."' ");
				}
                //DEBUG 获取用户所属网校
                $d_string_org = isset($_REQUEST['d_string_org']) && !empty($_REQUEST['d_string_org']) ? trim($_REQUEST['d_string_org'],','):'';
                if($d_string_org){
                    $d_array_org = explode(',',$d_string_org);
                    foreach($d_array_org AS $key => $value){
                        @DB::insert('org_user',array('ou_d_id'=>$value,'ou_user_id'=>$insert_user_id,'user_role_id'=>$_REQUEST['userInfo_user_role_id']));
                        //DEBUG 添加网校教师用户增加 添加签约表(apply_org)信息 
                        @DB::insert("apply_org",array('user_id'=>$insert_user_id,'d_id'=>$value,'dateline'=>TIMESTAMP,'flag'=>1));
                    }
                }
                
			}

			//DEBUG 接收用户信息 end
			$return_array = array("success"=>1,"message"=>"操作成功","data"=>array());
			echo json_encode($return_array);
			die;
		}else{
			$hschool_class = load_class('hschool','class');
			if($hschool_class){
				$class_array_all = $hschool_class->get_created_master_class();
			}
			$hschool_dept = load_class('hschool','dept');
			if($hschool_dept){
				$tree2array_dept_option=$hschool_dept->get_user_sub_tree2option();
				$user_sub_dept = $hschool_dept->get_user_sub_dept($_G['user_id']);
				if($_G['member']['dept_id']){
					$sql = "SELECT d_parent_id,d_level,d_name FROM ".DB::table('dictionary')." WHERE d_id='".$_G['member']['dept_id']."' LIMIT 1";
					$user_d_info = DB::fetch_first($sql);
					$user_d_parent_id = $user_d_info['d_parent_id'];
					$user_d_level = $user_d_info['d_level'];
				}
				//取出筛选项目
				$selected_d_level_2_dept_id = isset($_REQUEST['d_level_2_dept_id']) ? $_REQUEST['d_level_2_dept_id']:'';
				$selected_d_level_3_dept_id = isset($_REQUEST['d_level_3_dept_id']) ? $_REQUEST['d_level_3_dept_id']:'';
				if(3== $user_d_level){
					$selected_d_level_2_dept_id = $_G['member']['dept_id'];
				}
				if(4== $user_d_level){
					$selected_d_level_2_dept_id = $user_d_parent_id;
					$selected_d_level_3_dept_id = $_G['member']['dept_id'];
				}
			}
            $sql = "SELECT d_name,d_level FROM ".DB::table('dictionary')." WHERE d_id='".$_G['member']['dept_id']."' LIMIT 1";
            $user_info = DB::fetch_first($sql);
            $cache_subject_path = SITE_ROOT.'./data/sysdata/cache_subject.php';
            $subject = array();
            if(file_exists($cache_subject_path)){
                include $cache_subject_path;
            }
            //DEBUG 获取下属或我管理的网校 开始
            if($hschool_dept){
                $d_id_array=array();
                $d_id_array = $hschool_dept->get_sub_tree($_G['dept_id'],2);
                if(!empty($d_id_array)){
                    $d_id_array=array_keys($hschool_dept->tree2array($d_id_array));
                }
                if($_G['member']['is_dept_master']){
                    //$d_id_array[] = $_G['dept_id'];
                    //DEBUG 是否网校用户
                    $sql = "SELECT d_id FROM ".DB::table('dictionary')." WHERE d_id='".$_G['dept_id']."' AND d_type=2 LIMIT 1";
                    $d_result=DB::fetch_first($sql);
                    if($d_result){
                       $d_id_array[] = $d_result['d_id']; 
                    }
                    //DEBUG 取出用户管理机构 结束
                }
                //DEBUG 取出用户管理的网校 开始
                $sql = "SELECT ou_d_id FROM ".DB::table('org_user')." WHERE ou_user_id='".$_G['user_id']."' AND is_master=1 LIMIT 100";
                $d_org_result=DB::fetch_all($sql);
                if($d_org_result){
                    foreach($d_org_result AS $key =>$value){
                        $d_id_array[] = $value['ou_d_id'];
                    }
                }
                //DEBUG 取出用户管理的网校 结束
            }
            //DEBUG 过滤为发布网校
            $sql_check_status = "SELECT d_id FROM ".DB::table('school')." WHERE d_id IN (".dimplode($d_id_array).") AND school_status=0";
            $result_check = DB::fetch_all($sql_check_status);
            $d_id_array_status = array();
            foreach($result_check AS $key => $value){
                $d_id_array_status[$value['d_id']]=$value['d_id'];
            }
            $sql = "SELECT d_id,d_name FROM ".DB::table('dictionary')." WHERE d_id IN (".dimplode($d_id_array_status).") AND d_type=2 AND isdelete=0";
            $org_result = DB::fetch_all($sql);
            $org_json_string = '';
            foreach($org_result AS $key => $value){
                $org_json_string .= '{ "id":"'.$value['d_id'].'", pId:1, "name":"'.$value['d_name'].'"},';
            }
            $org_json_string = trim($org_json_string,',');
            if($org_json_string){
                $org_json_string=','.$org_json_string;
            }
            //DEBUG 获取下属或我管理的网校 结束
            //DEBUG 取出系统课程分类信息作为教师任课课程信息 开始
            $system_subject = load_class('system','subject');
            $cate_sub_tree = $cate_sub_array = array();
            $cate_sub_tree = $system_subject->get_sub_tree(200);
			$cate_sub_array = $system_subject->tree2array($cate_sub_tree);
            $cate_sub_json = '';
			//DEBUG 拼接前端部门树形JSON格式
			foreach($cate_sub_array AS $key=>$value){
				if(1 <= $value['d_level']){
					$is_open = '';
					if(2==$value['d_level']){
						$is_open = ',open:true';
					}
                    if(3 > $value['d_level']){
                        $is_open .= ', nocheck:true';
                    }
					$is_check='';
					if(in_array($value['d_id'],$dept_ids)){
						$is_check = ', checked:true';
					}
					$cate_sub_json .= '{ id:'.$value['d_id'].', pId:'.$value['d_parent_id'].', name:"'.$value['d_name'].'"'.$is_open.$is_check.'},';
				}
			}
			$cate_sub_json = trim($cate_sub_json,',');
            //DEBUG 取出系统课程分类信息作为教师任课课程信息 结束
			include template('admin/hschool/user_add');
		}
		break;

	case "detail":
		$hschool_dept = load_class('hschool','dept');
		//获取用户信息
		$user_info_array = array();
		$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
		if($id){
			$sql = "SELECT u.* from ".DB::table('users')." AS u WHERE user_id = '".$id."' LIMIT 1";
			$user_info_array = DB::fetch_first($sql);
		}
		//DEBUG 取出角色名称
		if($user_info_array['user_role_id']){
			$user_info_array['user_role_name'] = ext::role_name($user_info_array['user_role_id']);
		}
		//DEBUG 取出所教授的学科
		if(strpos($user_info_array['subjects'],',') && !empty($user_info_array['subjects'])){
			$subjects_array = explode(',', $user_info_array['subjects']);
			if($hschool_dept){
				$cate_ids_array = $subjects_name_array = $hschool_dept->get_multi_info($subjects_array);
				$user_info_array['subjects_name'] = implode(' ',$subjects_name_array);
				$user_info_array['subjects_id_val'] = str_replace(',',' ',$user_info_array['subjects']);
			}
		}else{
			if($hschool_dept){
				$subjects_name_info = $hschool_dept->get_info($user_info_array['subjects']);
				$user_info_array['subjects_name'] = $subjects_name_info['d_name'];
				$user_info_array['subjects_id_val'] = $user_info_array['subjects'];
                $cate_ids_array[$user_info_array['subjects_id_val']]=$subjects_name_info['d_name'];
			}
		}
		switch($user_info_array['user_role_id']){
			case "21":
				//DEBUG 学生 取出家长信息
				//DEBUG 取出机构路径 从第一到用户所在级别
				$hschool_dept = load_class('hschool','dept');
				if($hschool_dept){
					$user_info_array['user_depts']=$hschool_dept->get_user_depts($user_info_array['dept_id']);
				}
				$sql = "SELECT u.* from ".DB::table('users')." AS u WHERE user_id = '".$user_info_array['parent_user_id']."' LIMIT 1";
				$parent_user_info = DB::fetch_first($sql);
				$user_info_array['parent_user_info'] = $parent_user_info;
				//DEBUG 取出班级名称
				$user_info_array['class_name'] = ext::class_name($user_info_array['class_id']);
				break;
			case "22":
				//DEBUG 老师 取出其所在班级与任课信息
				//DEBUG 取出机构路径 从第一到用户所在级别
				$hschool_dept = load_class('hschool','dept');
				if($hschool_dept){
					$user_info_array['user_depts']=$hschool_dept->get_user_depts($user_info_array['dept_id']);
				}
				$sql = "SELECT um.* from ".DB::table('class_member')." AS um WHERE um.isdelete=0 AND um.user_id = '".$user_info_array['user_id']."'";
				$class_info = DB::fetch_all($sql);
				foreach($class_info AS $key => $value){
					$class_info[$key]['class_name']= ext::class_name($value['class_id']);
					$subject_d_ids = strpos($value['subject_d_ids'], ',') && !empty($value['subject_d_ids']) ? explode(',', $value['subject_d_ids']) : $value['subject_d_ids'];
					$class_info[$key]['class_subjects_info']= ext::d_name($subject_d_ids);
					if(is_array($class_info[$key]['class_subjects_info'])){
						$class_info[$key]['class_subjects_info_string']= implode(',',$class_info[$key]['class_subjects_info']);
					}else{
						$class_info[$key]['class_subjects_info_string']= $class_info[$key]['class_subjects_info'];
					}
				}
				$user_info_array['class_info'] = $class_info;
                //DEBUG 取出系统课程分类信息作为教师任课课程信息 开始
                $system_subject = load_class('system','subject');
                $cate_sub_tree = $cate_sub_array = array();
                $cate_sub_tree = $system_subject->get_sub_tree(200);
                $cate_sub_array = $system_subject->tree2array($cate_sub_tree);
                $cate_sub_json = '';
                //DEBUG 拼接前端部门树形JSON格式
                foreach($cate_sub_array AS $key=>$value){
                    if(1 <= $value['d_level']){
                        $is_open = '';
                        if(2==$value['d_level']){
                            $is_open = ',open:true';
                        }
                        if(3 > $value['d_level']){
                            $is_open .= ', nocheck:true';
                        }
                        $is_check='';
                        if(in_array($value['d_id'],$subjects_array)){
                            $is_check = ', checked:true';
                        }
                        $cate_sub_json .= '{ id:'.$value['d_id'].', pId:'.$value['d_parent_id'].', name:"'.$value['d_name'].'"'.$is_open.$is_check.'},';
                    }
                }
                $cate_sub_json = trim($cate_sub_json,',');
                //DEBUG 取出系统课程分类信息作为教师任课课程信息 结束
				break;
			case "23":
				//DEBUG 家长 取出子女信息
				$sql = "SELECT u.* from ".DB::table('users')." AS u WHERE parent_user_id = '".$user_info_array['user_id']."'";
				$child_user_info = DB::fetch_all($sql);
				$user_info_array['child_user_info'] = $child_user_info;
				//DEBUG 取出子女班级名称
				foreach($user_info_array['child_user_info'] AS $key => $value){
					$user_info_array['child_user_info'][$key]['class_name'] = ext::class_name($value['class_id']);
				}
				break;
			case "31":
				//DEBUG 机构
				//DEBUG 取出机构路径 从第一到用户所在级别
				$hschool_dept = load_class('hschool','dept');
				if($hschool_dept){
					$user_info_array['user_depts']=$hschool_dept->get_user_depts($user_info_array['dept_id']);
				}
				break;
			case "32":
				//DEBUG 教研员
				//DEBUG 取出机构路径 从第一到用户所在级别
				$hschool_dept = load_class('hschool','dept');
				if($hschool_dept){
					$user_info_array['user_depts']=$hschool_dept->get_user_depts($user_info_array['dept_id']);
				}
				break;
            default:
                //DEBUG 其他类型用户
				//DEBUG 取出机构路径 从第一到用户所在级别
				$hschool_dept = load_class('hschool','dept');
				if($hschool_dept){
					$user_info_array['user_depts']=$hschool_dept->get_user_depts($user_info_array['dept_id']);
				}
                break;
		}
        $cache_subject_path = SITE_ROOT.'./data/sysdata/cache_subject.php';
        $subject = array();
        if(file_exists($cache_subject_path)){
            include $cache_subject_path;
        }
        //DEBUG 取出用户所在网校 开始
        $sql = "SELECT ou_d_id FROM ".DB::table('org_user')." WHERE ou_user_id='".$id."'";
        $d_org_result=DB::fetch_all($sql);
        if($d_org_result){
            foreach($d_org_result AS $key =>$value){
                $d_id_array[$value['ou_d_id']] = $value['ou_d_id'];
            }
        }
        $user_org_array = ext::d_name($d_id_array);
        $user_org_string = '';
        foreach($user_org_array AS $key => $value){
            $user_org_string .=  $key.',';
        }
        $user_org_string =  trim($user_org_string,',');
        //DEBUG 取出用户所在网校 结束
        //DEBUG 获取下属或我管理的网校 开始
        if($hschool_dept){
            $d_id_array=array();
            $d_id_array = $hschool_dept->get_sub_tree($_G['dept_id'],2);
            if(!empty($d_id_array)){
                $d_id_array=array_keys($hschool_dept->tree2array($d_id_array));
            }
            if($_G['member']['is_dept_master']){
                //$d_id_array[] = $_G['dept_id'];
                //DEBUG 是否网校用户
                $sql = "SELECT d_id FROM ".DB::table('dictionary')." WHERE d_id='".$_G['dept_id']."' AND d_type=2 LIMIT 1";
                $d_result=DB::fetch_first($sql);
                if($d_result){
                   $d_id_array[] = $d_result['d_id']; 
                }
                //DEBUG 取出用户管理机构 开始
                $sql = "SELECT ou_d_id FROM ".DB::table('org_user')." WHERE ou_user_id='".$_G['user_id']."'";
                $d_org_result=DB::fetch_all($sql);
                if($d_org_result){
                    foreach($d_org_result AS $key =>$value){
                        $d_id_array[] = $value['ou_d_id'];
                    }
                }
                //DEBUG 取出用户管理机构 结束
            }
        }
        //DEBUG 过滤为发布网校
        $sql_check_status = "SELECT d_id FROM ".DB::table('school')." WHERE d_id IN (".dimplode($d_id_array).") AND school_status=0";
        $result_status = DB::fetch_all($sql_check_status);
        $d_id_array_status = array();
        foreach($result_status AS $key => $value){
            $d_id_array_status[$value['d_id']]=$value['d_id'];
        }
        $sql = "SELECT d_id,d_name FROM ".DB::table('dictionary')." WHERE d_id IN (".dimplode($d_id_array_status).") AND d_type=2 AND isdelete=0";
        $org_result = DB::fetch_all($sql);
        $org_json_string = '';
        foreach($org_result AS $key => $value){
            $is_check = '';
            if(!empty($user_org_array[$value['d_id']])){
                $is_check = ', checked:true';
            }
            $org_json_string .= '{ "id":"'.$value['d_id'].'", pId:1, "name":"'.$value['d_name'].'"'.$is_check.'},';
        }
        $org_json_string = trim($org_json_string,',');
        if($org_json_string){
            $org_json_string=','.$org_json_string;
        }
        //DEBUG 获取下属或我管理的网校 结束
		include template('admin/hschool/user_detail');
		break;

	case "delete":
		//DEBUG 删除用户信息
		$user_ids_array = array();
		$user_ids_array = isset($_REQUEST['user_ids']) ? $_REQUEST['user_ids']:array();
		if($user_ids_array){
			$update = array(
				'isdelete'=> 1
			);
			$where = 'user_id IN ('.dimplode($user_ids_array).')';
			$effects = DB::update('users',$update,$where);
			$effects = DB::update('class_member',$update,$where);
            //DEBUG 关联取消 名师推荐 网校签约与关系 开始
            foreach($user_ids_array AS $key => $value){
                @DB::delete('famous_teachers',array('teacher_id'=>$value),1);
                @DB::delete('org_user',array('ou_user_id'=>$value));
                @DB::delete('apply_org',array('user_id'=>$value));
            }
            //DEBUG 关联取消 名师推荐 网校签约与关系 结束
		}
		//DEBUG 接收用户信息 end
		$return_array = array("success"=>1,"message"=>"操作成功","data"=>array());
		echo json_encode($return_array);
		break;

	case "password_reset":
		//DEBUG 重置用户密码为 123456
		$user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id']:'';
		if($user_id){
			$data = array(
				'user_password'=> encode_password('123456')
			);
			$where = " user_id = '".$user_id."' ";
			$result = DB::update('users',$data,$where);
			$return_array = array("error"=>false,"message"=>"密码成功修改为 123456");
		}else{
			$return_array = array("error"=>true,"message"=>"修改失败,请选择用户重新操作");
		}
		echo json_encode($return_array);
		break;

	case "index":
		//DEBUG 所有前端角色 学生 老师 家长
		$page_array = array();
		$sqlcount = $sql = $wheresql = $title_keyword = $pageNum = $numPerPage = '';
		$pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
		$numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 10;
		$pagestart = ($pageNum - 1) * $numPerPage;
		$user_role_id = isset($_REQUEST['userInfo_user_role_id']) ? $_REQUEST['userInfo_user_role_id'] : '';
		$user_phone = isset($_REQUEST['userInfo_user_phone']) ? $_REQUEST['userInfo_user_phone'] : '';
		$user_id = isset($_REQUEST['userInfo_user_id']) ? $_REQUEST['userInfo_user_id'] : '';
		$user_realname = isset($_REQUEST['userInfo_user_realname']) ? $_REQUEST['userInfo_user_realname'] : '';
        $user_name = isset($_REQUEST['userInfo_user_name']) ? $_REQUEST['userInfo_user_name'] : '';
		//DEBUG 分级别管理用户 取出用户的子部门,如果是部门管理员则带上本部门编号
		$hschool_dept = load_class('hschool','dept');
		if($hschool_dept){
			$d_id_array=array();
			$d_id_array = $hschool_dept->get_sub_tree($_G['dept_id']);
			if(!empty($d_id_array)){
				$d_id_array=array_keys($hschool_dept->tree2array($d_id_array));
			}
			if($_G['member']['is_dept_master']){
				$d_id_array[] = $_G['dept_id'];
			}
		}

		//DEBUG 取出用户所属网校
		$org_user_sql = "SELECT ou_d_id FROM ".DB::table('org_user')." WHERE ou_user_id = '".$_G['user_id']."'";
		$org_user_array = DB::fetch_all($org_user_sql);
		$org_user_d_id = array();
        if(1 != $_G['user_id']){
        	foreach($org_user_array AS $key => $value){
                $org_user_d_id[$value['ou_d_id']] = $value['ou_d_id'];
            }
            if(!empty($org_user_d_id)){
                if($d_id_array){
                    $wheresql .= " AND ( ou.ou_d_id IN (".dimplode($org_user_d_id).") OR dept_id IN (".dimplode($d_id_array).") )";
                }else{
                    $wheresql .= " AND ou.ou_d_id IN (".dimplode($org_user_d_id).") ";
                }
            }else{
                if($d_id_array){
                    $wheresql .= " AND dept_id IN (".dimplode($d_id_array).")";
                }
            }   
        }
		if($user_role_id){
			$wheresql .= " AND u.user_role_id = '".$user_role_id."'";
		}
		if($user_phone){
			$wheresql .= " AND u.user_phone = '".$user_phone."'";
		}
		if($user_id){
			$wheresql .= " AND u.user_id = '".$user_id."'";
		}
		if($user_realname){
			$wheresql .= " AND u.user_realname LIKE '%".$user_realname."%'";
		}
        if($user_name){
			$wheresql .= " AND u.user_name = '".$user_name."'";
		}
		$sqlcount = "SELECT count(*) from ".DB::table('users')." AS u LEFT JOIN ".DB::table('org_user')." AS ou ON u.user_id = ou.ou_user_id WHERE u.isdelete=0 ".$wheresql;
		$pagetotal = DB::result_first($sqlcount);
		$sql = "SELECT u.*,ou.ou_d_id from ".DB::table('users')." AS u LEFT JOIN ".DB::table('org_user')." AS ou ON u.user_id = ou.ou_user_id WHERE u.isdelete=0 ".$wheresql." ORDER BY u.user_id DESC LIMIT ".$pagestart.",".$numPerPage;
		$page_array = DB::fetch_all($sql);
        foreach($page_array AS $key => $value){
            $page_array[$key]['net_school_name']=ext::d_name($value['ou_d_id']);
        }
		//总页数
		$realpages = @ceil($pagetotal / $numPerPage);
		if($realpages <= 0){
			$realpages=1;
		}
		//前一页
		$page_pre = max(1, intval(($pageNum-1)));
		//后一页
		$page_next = min(intval(($pageNum+1)), $realpages);
		include template('admin/hschool/user');
		break;
    case 'index_export':
        //ini_set('display_errors','On');
        //error_reporting(E_ALL);
        ini_set('memory_limit', '1024M');
		$page_array = array();
        $d_id_array_string = isset($_REQUEST['d_id_array_string']) ? $_REQUEST['d_id_array_string'] : '';
        if($d_id_array_string){
            $d_id_array_array = explode(',',$d_id_array_string);
            if(!empty($d_id_array_array)){
                $wheresql = " AND u.dept_id IN (".dimplode($d_id_array_array).") ";
            }
        }
		//DEBUG 增加排序字段 开始
		//DEBUG 增加排序字段 结束
        //姓名、帐号、学籍号、学校名称、班级、区县
        $sql = "SELECT dd.d_name AS d_name_area,d.d_name AS d_name_dept,u.user_name,u.user_realname,c.class_name,u.user_phone,u.student_no 
            FROM ".DB::table('users')." AS u 
            LEFT JOIN ".DB::table('class')." AS c ON u.class_id=c.class_id
            LEFT JOIN ".DB::table('dictionary')." AS d ON u.dept_id=d.d_id 
            LEFT JOIN ".DB::table('dictionary')." AS dd ON d.d_parent_id=dd.d_id 
            WHERE u.user_role_id=21 AND u.isdelete=0 AND user_id > 1000 ".$wheresql." ORDER BY user_id DESC";
        $page_array = DB::fetch_all($sql);
		foreach($page_array AS $key => $value){
            $stu_data[$key][0]=$value['user_realname'];
            $stu_data[$key][1]="'".$value['user_name'];
            $stu_data[$key][2]=$value['student_no'];
            $stu_data[$key][3]=$value['d_name_dept'];
            $stu_data[$key][4]=$value['class_name'];
            $stu_data[$key][5]=$value['d_name_area'];
		}
        $i=0;
        $csv_data[$i] = array(0=>"姓名",1=>"帐号",2=>"学籍号",3=>"学校名称",4=>"班级",5=>"区县");
        foreach($stu_data AS $k => $v){
            $i++;
            $csv_data[$i] = array(0=>$v[0],1=>$v[1],2=>$v[2],3=>$v[3],4=>$v[4],5=>$v[5]);
        }
        $csv_name = date('YmdHis',TIMESTAMP).'.csv';
        $csv = new csv($csv_name,$csv_data);
        $csv->csv_to_httpd();
        break;
}
?>